using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;

public class DataScreen : MonoBehaviour
{
    [SerializeField] private Material m_LitMat;
    [SerializeField] private PostFXSettings m_PostProcessingSetting;
    private VisualElement m_RootElement;
    private Slider m_SmoothSlider;
    private Slider m_MetallicSlider;
    private Toggle m_ShadowToggle;
    private Toggle m_ClearCoatToggle;
    private Toggle m_MultiscatteringToggle;
    
    private Toggle m_SpecularToggle;
    private Toggle m_IBLToggle;
    static class ShaderProperty
    {
        public static readonly int _Smooth = Shader.PropertyToID("_Smoothness");
        public static readonly int _Metallic = Shader.PropertyToID("_Metallic");
        public static readonly string _PCFShadow = new string("V_SHADOW_PCF");
        public static readonly string _ClearCoat = new string("_CLEARCOAT");
        public static readonly string _Multiscattering = new string("_MULTISCATTERING");
        
            
        public static readonly string _NOSpecularactor = new string("_NOSpecularactor");
        public static readonly string _NOIBL = new string("_NOIBL");

    }
    void Awake()
    {
        
        
        m_RootElement = GetComponent<UIDocument>().rootVisualElement;
        m_SmoothSlider=m_RootElement.Q<Slider>("Smooth");
        m_MetallicSlider=m_RootElement.Q<Slider>("Metallic");
        m_ShadowToggle=m_RootElement.Q<Toggle>("Shadow");
        m_ClearCoatToggle=m_RootElement.Q<Toggle>("ClearCoat");
        m_MultiscatteringToggle=m_RootElement.Q<Toggle>("Multiscattering");
        m_SpecularToggle=m_RootElement.Q<Toggle>("Specular");
        m_IBLToggle=m_RootElement.Q<Toggle>("IBL");
        
        m_SmoothSlider.SetValueWithoutNotify(m_LitMat.GetFloat(ShaderProperty._Smooth));
        m_SmoothSlider.RegisterValueChangedCallback(OnSmoothChanged);
        //_RECEIVE_SHADOWS_OFF
        m_MetallicSlider.SetValueWithoutNotify(m_LitMat.GetFloat(ShaderProperty._Metallic));
        m_MetallicSlider.RegisterValueChangedCallback(OnMetallicChanged);
        //Shadow
        m_ShadowToggle.SetValueWithoutNotify(m_LitMat.IsKeywordEnabled(ShaderProperty._PCFShadow));
        m_ShadowToggle.RegisterValueChangedCallback(OnShadowChanged);
        m_ClearCoatToggle.SetValueWithoutNotify(m_LitMat.IsKeywordEnabled(ShaderProperty._ClearCoat));
        m_ClearCoatToggle.RegisterValueChangedCallback(OnClearCoatChanged);
        m_MultiscatteringToggle.SetValueWithoutNotify(m_LitMat.IsKeywordEnabled(ShaderProperty._Multiscattering));
        m_MultiscatteringToggle.RegisterValueChangedCallback(OnMultiscatteringChanged);
        
        m_SpecularToggle.SetValueWithoutNotify(!m_LitMat.IsKeywordEnabled(ShaderProperty._NOSpecularactor));
        m_SpecularToggle.RegisterValueChangedCallback(OnSpecularToggleChanged);
        m_IBLToggle.SetValueWithoutNotify(!m_LitMat.IsKeywordEnabled(ShaderProperty._NOIBL));
        m_IBLToggle.RegisterValueChangedCallback(OnIBLToggleChanged);

    }

    private void OnEnable()
    {
        m_RootElement = GetComponent<UIDocument>().rootVisualElement;
        
        m_SmoothSlider=m_RootElement.Q<Slider>("Smooth");
        m_MetallicSlider=m_RootElement.Q<Slider>("Metallic");
        m_ShadowToggle=m_RootElement.Q<Toggle>("Shadow");
        m_ClearCoatToggle=m_RootElement.Q<Toggle>("ClearCoat");
        m_MultiscatteringToggle=m_RootElement.Q<Toggle>("Multiscattering");
        m_SpecularToggle=m_RootElement.Q<Toggle>("Specular");
        m_IBLToggle=m_RootElement.Q<Toggle>("IBL");
        
        m_SmoothSlider.SetValueWithoutNotify(m_LitMat.GetFloat(ShaderProperty._Smooth));
        
        //_RECEIVE_SHADOWS_OFF
        m_MetallicSlider.SetValueWithoutNotify(m_LitMat.GetFloat(ShaderProperty._Metallic));
        
        //Shadow
        m_ShadowToggle.SetValueWithoutNotify(m_LitMat.IsKeywordEnabled(ShaderProperty._PCFShadow));
        
        m_ClearCoatToggle.SetValueWithoutNotify(m_LitMat.IsKeywordEnabled(ShaderProperty._ClearCoat));
        
        m_MultiscatteringToggle.SetValueWithoutNotify(m_LitMat.IsKeywordEnabled(ShaderProperty._Multiscattering));
        
        
        m_SpecularToggle.SetValueWithoutNotify(!m_LitMat.IsKeywordEnabled(ShaderProperty._NOSpecularactor));
        
        m_IBLToggle.SetValueWithoutNotify(!m_LitMat.IsKeywordEnabled(ShaderProperty._NOIBL));
        
        
        bool flag=m_SmoothSlider.RegisterValueChangedCallback(OnSmoothChanged);
        m_MetallicSlider.RegisterValueChangedCallback(OnMetallicChanged);
        m_ShadowToggle.RegisterValueChangedCallback(OnShadowChanged);
        m_ClearCoatToggle.RegisterValueChangedCallback(OnClearCoatChanged);
        m_MultiscatteringToggle.RegisterValueChangedCallback(OnMultiscatteringChanged);
        m_SpecularToggle.RegisterValueChangedCallback(OnSpecularToggleChanged);
        m_IBLToggle.RegisterValueChangedCallback(OnIBLToggleChanged);
    }

    private void OnIBLToggleChanged(ChangeEvent<bool> evt)
    {
        if (evt.newValue)
        {
            m_LitMat.DisableKeyword(ShaderProperty._NOIBL);
        }
        else
        {
            m_LitMat.EnableKeyword(ShaderProperty._NOIBL);
            
        }
    }

    private void OnSpecularToggleChanged(ChangeEvent<bool> evt)
    {
        if (evt.newValue)
        {
            m_LitMat.DisableKeyword(ShaderProperty._NOSpecularactor);
        }
        else
        {
            m_LitMat.EnableKeyword(ShaderProperty._NOSpecularactor);
            
        }
    }

    public void OnSmoothChanged(ChangeEvent<float> newValue)
    {
        m_LitMat.SetFloat(ShaderProperty._Smooth,newValue.newValue);
    }
    public void OnMetallicChanged(ChangeEvent<float> newValue)
    {
        m_LitMat.SetFloat(ShaderProperty._Metallic,newValue.newValue);
    }
    public void OnShadowChanged(ChangeEvent<bool> newValue)
    {
        if (newValue.newValue)
        {
            m_LitMat.EnableKeyword(ShaderProperty._PCFShadow);
        }
        else
        {
            m_LitMat.DisableKeyword(ShaderProperty._PCFShadow);
        }
    }
    public void OnClearCoatChanged(ChangeEvent<bool> newValue)
    {
        if (newValue.newValue)
        {
            m_LitMat.EnableKeyword(ShaderProperty._ClearCoat);
        }
        else
        {
            m_LitMat.DisableKeyword(ShaderProperty._ClearCoat);
        }
    }
    public void OnMultiscatteringChanged(ChangeEvent<bool> newValue)
    {
        if (newValue.newValue)
        {
            m_LitMat.EnableKeyword(ShaderProperty._Multiscattering);
        }
        else
        {
            m_LitMat.DisableKeyword(ShaderProperty._Multiscattering);
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            m_RootElement.style.display=
                m_RootElement.style.display==DisplayStyle.Flex?DisplayStyle.None:DisplayStyle.Flex;
        }
        else if (Input.GetKeyDown(KeyCode.Z))
        {
            m_PostProcessingSetting.Bloom.Enable = false;
        }
        
    }
}
